package com.dgshanger.sanhxiaofeisc.mvp.presenter;

import com.dgshanger.sanhxiaofeisc.entity.UserEntity;
import com.dgshanger.sanhxiaofeisc.mvp.contract.PersonContract;
import com.dgshanger.sanhxiaofeisc.mvp.model.PersonModel;
import com.xx.baseuilibrary.mvp.BaseRxMvpPresenter;
import com.xx.baseutilslibrary.network.rx.RxHttpObserver;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;

/**
 * PersonPresenter
 * 沉迷学习不能自拔
 * Describe：
 * Created by 雷小星🍀 on 2018/4/26 17:06.
 */

public class PersonPresenter extends BaseRxMvpPresenter<PersonContract.Model, PersonContract.View> implements PersonContract.Presenter {

    @NotNull
    @Override
    protected PersonContract.Model createModel() {
        return new PersonModel();
    }

    @Override
    public void getUserInfo() {

        getModel().getUserInfo(new RxHttpObserver<UserEntity>() {

            @Override
            public void onLoadingStart() {
                attachObserver(this);
                if (getView() != null) {
                    getView().setRefreshing(true);
                }
            }

            @Override
            public void onLoadingFinish() {
                if (getView() != null) {
                    getView().setRefreshing(false);
                }
            }

            @Override
            public void onCompleted(@Nullable String msg, @Nullable UserEntity entity) {
                if (getView() != null) {
                    getModel().putUserEntity(entity);
                    getView().setData(entity);
                }
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() != null) {
                    getView().showError(new Throwable(error), false);
                }
            }
        });
    }

    @Override
    public void fileStore(File file) {
        getModel().fileStore(file, new RxHttpObserver<Object>() {
            @Override
            public void onCompleted(@Nullable String msg, @Nullable Object entity) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(msg);
                getView().editAvatarSuccess();
            }

            @Override
            public void onError(@Nullable String error) {
                if (getView() == null) {
                    return;
                }
                getView().showToast(error);
            }

            @Override
            public void onLoadingStart() {
                attachObserver(this);
                if (getView() == null) {
                    return;
                }
                getView().showLoadingDialog("头像上传中");
            }

            @Override
            public void onLoadingFinish() {
                if (getView() == null) {
                    return;
                }
                getView().dismissLoadingDialog();
            }
        });
    }
}
